
#include "stdafx.h"
#include "ZipParser.h"


CStringW CZipParser::GetConfigurationDocumentPath()
{
	//Locate the configuration document in the same directory
	//that this object is running from, the file name
	//is the same as our file with .DLL replaced with .xml
	//a sample configuration is included with this project

	CStringW returnValue;
	::GetModuleFileName( _AtlBaseModule.m_hInst, returnValue.GetBuffer( MAX_PATH ), MAX_PATH );
	returnValue.ReleaseBuffer();
	int posPeriod = returnValue.ReverseFind( _T('.') );
	returnValue = returnValue.Mid(0, posPeriod + 1 );
	returnValue += _T("xml");
	return returnValue;
}


void CZipParser::ReadConfiguration()
{
	//We are going to look in the configuration file
//if there is a configuration for another ZIP
//parser then we will read it and use it if 
//our parser does not understand the file
//format for the entries is documented in the 
//sample configuration file
	if (m_XMLConfiguration)
	{
		MSXML2::IXMLDOMNodePtr parserNode = m_XMLConfiguration->selectSingleNode(L"//ZipParser/SecondaryParser");
		if(parserNode)
		{
			CStringW objectCLSID;
			CStringW objectProgID;
			CStringW objectDLLPath((BSTR) parserNode->text);
			MSXML2::IXMLDOMNodePtr clsidNode =  parserNode->attributes->getNamedItem(L"CLSID");
			if(clsidNode)
			{
				objectCLSID = CStringW ((BSTR) clsidNode->text);
			}
			MSXML2::IXMLDOMNodePtr progidNode =  parserNode->attributes->getNamedItem(L"ProgID");
			if(progidNode)
			{
				objectProgID = CStringW ((BSTR) progidNode->text);
			}
			CLSID objectCoCLSID = {0};
			if(objectCLSID.IsEmpty())
			{
				::CLSIDFromProgID(objectProgID.GetBuffer(),&objectCoCLSID);
			}
			else
			{
				::CLSIDFromString(objectCLSID.GetBuffer(),&objectCoCLSID);
			}
			if(objectDLLPath.IsEmpty())
			{
				LoadOtherParser(objectCoCLSID,objectDLLPath);
			}
			else
			{
				LoadOtherParser(objectCoCLSID);
			}
		}

		m_AlwaysReloadConfigXML = false;
		m_AlwaysCallOtherZipParser = false;
		MSXML2::IXMLDOMNodePtr runtimeNode = m_XMLConfiguration->selectSingleNode(L"//ZipParser/Runtime");
		if(runtimeNode)
		{
			MSXML2::IXMLDOMNodePtr cacheConfigNode =  runtimeNode->attributes->getNamedItem(L"CacheParserConfiguration");
			if(cacheConfigNode)
			{
				CStringW cacheValue((BSTR) cacheConfigNode->text);
				cacheValue.MakeLower().Trim();
				if(L"false" == cacheValue) m_AlwaysReloadConfigXML = true;
			}
			if(NULL != m_OtherZipParser)
			{
				MSXML2::IXMLDOMNodePtr forceChainingNode =  runtimeNode->attributes->getNamedItem(L"ForceParserChaining");
				if(forceChainingNode)
				{
					CStringW forceChaining((BSTR) forceChainingNode->text);
					forceChaining.MakeLower().Trim();
					if(L"true" == forceChaining) m_AlwaysCallOtherZipParser = true;
				}
			}
		}

	}
}